﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class display_visitor : System.Web.UI.Page
{
    string s_where="", s_order;
    string curindex = String.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        Session["sTittle"] = "察看访问记录";
        if (Session["sRole"] != null&&Session["sRole"].ToString()!="管理员")
        {
            //SDSvisitor.FilterExpression = string.Format("jigou='{0}'", Session["sJigou"].ToString());
          
           ddlVisible.Visible = false;
        }
        if (!IsPostBack)
        {
            DataBrid();
        }
      

    }
    //绑定数据 
    private void DataBrid()
    {
        int record = 0;
        int count = 0;
        s_order = "order by [xtsj] desc";
        if (Session["sRole"] != null && Session["sRole"].ToString() == "管理员")
        {
            s_where = " where 1=1 ";
        }
        else
        {
            s_where = " where 1=1 and jigou =" + "'" + Session["sjigou"].ToString() + "'";
        }
       
        string sqlText = "*";
        DataTable DT = DbHelper.ExecutePage(sqlText, "[details]" + s_where, "id", s_order, Pager.CurrentPageIndex, Pager.PageSize, out record, out count, null);
        Pager.RecordCount = record;
        this.GridView2.DataSource = DT;
        this.GridView2.DataBind();
    }


    //带条件绑定数据 
    private void DataBrid(string s_where)
    {
        int record = 0;
        int count = 0;
        s_order = "order by [xtsj] desc";
        string sqlText = "*";
        DataTable DT = DbHelper.ExecutePage(sqlText, "[details]" + s_where, "id", s_order, Pager.CurrentPageIndex, Pager.PageSize, out record, out count, null);
        Pager.RecordCount = record;
        this.GridView2.DataSource = DT;
        this.GridView2.DataBind();
    }



    protected void Button3_Click(object sender, EventArgs e)
    {
        Conn zconn = new Conn();
        int num = 0;
        for (int i = 0; i <= GridView2.Rows.Count - 1; i++)
        {
            CheckBox cbox = (CheckBox)GridView2.Rows[i].FindControl("CheckBox1");
            if (cbox.Checked == true)
            {
                string sqlstr = "delete from details where id=" + GridView2.DataKeys[i].Value;
                int noth = zconn.ExeSql(sqlstr);
                if (noth == 1&&num==0)
                {
                    num++;
                    string msg="alert('删除成功');";
                    ClientScript.RegisterClientScriptBlock(this.GetType(), "script" + i, msg,true);
                }
                else if(num==0)
                {
                    num++;
                    string msg = "alert('删除失败');";
                    ClientScript.RegisterStartupScript(this.GetType(), "script" + i, msg,true);
                }
            }
        }
        zconn.Dispose();
        DataBrid();
    }
    protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //if (e.Row.RowIndex != -1)
        //{
        //    int id = e.Row.RowIndex + 1;
        //    e.Row.Cells[0].Text = id.ToString();
        //}
        //if (e.Row.RowType == DataControlRowType.DataRow)
        //{
        //    //当鼠标停留时更改背景色
        //    e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#80BA21'");
        //    //当鼠标移开时还原背景色
        //    e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
        //}
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        if (this.DropDownList2.SelectedValue != string.Empty)
        {
            s_where = string.Format(" where username like '%{0}%' and jigou= '{1}'", txtUser.Text.Trim(), this.DropDownList2.SelectedValue);           
        }
        else
        {
            s_where = string.Format(" where username like '%{0}%' and jigou ='{1}'", txtUser.Text.Trim(), Session["sjigou"].ToString());         
        }
        Pager.CurrentPageIndex = 0;
        ViewState["s_where"] = s_where;
        DataBrid(s_where);
    }
    protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (this.DropDownList2.SelectedValue != string.Empty)
        {
            s_where = string.Format(" where jigou= '{0}'", this.DropDownList2.SelectedValue);
            Pager.CurrentPageIndex = 0;
            ViewState["s_where"] = s_where;
            DataBrid(s_where);
        }
        else
        {
            Pager.CurrentPageIndex = 0;
            DataBrid();
        }
        this.txtUser.Text = "";
      
    }

    protected void pager_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
    {
        Pager.CurrentPageIndex = e.NewPageIndex;
        if (ViewState["s_where"] != null)
        {
            DataBrid(ViewState["s_where"].ToString());
        }
        else
        {
            DataBrid();
        }

    }

}
